import { Component, OnInit, Output, EventEmitter } from "@angular/core";
import { UserService } from "./../user.service";
import "./wwLogin.js";

interface QywechatType {
  agentid: string;
  appid: string;
  redirect_uri: string;
  state: string;
}


@Component({
  selector: "qywechat",
  templateUrl: "qywechat.component.html",
  styleUrls: ["qywechat.component.less"]
})
export class QyWechatComponent implements OnInit {
  @Output() setModal = new EventEmitter();
  qywx_login_config: QywechatType;
  constructor(private userService: UserService) {
    if (!this.qywx_login_config) {
      this.userService.getQyWechat().subscribe(data => {
        if (data && data.code === 0 ) {
          this.qywx_login_config = data.data;
          this._showQyWxLogin();
        } else {
          console.log("get qywechat err" + data);
        }
      });
    } else {
        this._showQyWxLogin();
    }
  }

  ngOnInit() { }

  changeModal(action: string) {
    this.setModal.emit(action);
  }

  private _showQyWxLogin() {
    window.WwLogin({
      "id": "qywx_login_frame",
      "appid": this.qywx_login_config.appid,
      "agentid": this.qywx_login_config.agentid,
      "redirect_uri": this.qywx_login_config.redirect_uri,
      "state": this.qywx_login_config.state,
      "href": "",
  });
  }
}